import { sjhq, sjlr, sjsc } from '@/services';
import ProList from '@ant-design/pro-list';
import { ActionType } from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Space, Typography } from 'antd';
import { useRef, useState } from 'react';
import { history } from 'umi';

export default () => {
  const actionRef = useRef<ActionType>();
  const [form] = Form.useForm();

  const query = async (params: any) => {
    const data = await sjhq();
    if (data.msg === '失败') {
      message.error(data.content);
      return {
        data: []
      }
    }
    return {
      data: data.content,
    };
  };

  const handleOk = async () => {
    const values = await form.validateFields();
    const data = await sjlr(values);
    if (data.msg === '失败') {
      return  message.error(data.content);
    }
    actionRef.current?.reload();
  };

  const onDel = async (time: string) => {
    Modal.confirm({
      content: '确认删除会计期间？',
      onOk: async () => {
        const data = await sjsc({ time });
        if (data.msg === '失败') return  message.error(data.content);
        actionRef.current?.reload?.();
      },
    });
  };

  return (
    <div style={{ display: 'flex', height: '100%' }}>
      <div
        style={{
          width: '100%',
          height: '100%',
          marginRight: '8px',
          background: '#fff',
        }}
      >
        <ProList
          actionRef={actionRef}
          defaultSize="small"
          toolBarRender={() => {
            return [
              <Form layout="inline" form={form}>
                <Form.Item name="time" label="会计期间（年）">
                  <Input />
                </Form.Item>
              </Form>,
              <Button key="add" type="primary" onClick={handleOk}>
                新建期间
              </Button>,
            ];
          }}
          onRow={({ time, suan }) => {
            return {
              onClick: () => {
                // suan 标识是否已经被计算了
                history.push(`/time/lr/${time}?canCalc=${suan ? 0 : 1}`);
              },
            };
          }}
          rowKey="time"
          headerTitle="新建会计期间"
          request={query}
          showActions="always"
          metas={{
            title: {
              dataIndex: 'time',
            },
            actions: {
              render: (_, record) => (
                <a onClick={() => onDel(record.time)}>删除会计期间</a>
              ),
            },
          }}
        />
      </div>
    </div>
  );
};
